package com.whoiszxl.query.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whoiszxl.core.utils.BeanUtil;
import com.whoiszxl.db.mapper.MemberAddressMapper;
import com.whoiszxl.db.model.MemberAddressPO;
import com.whoiszxl.dto.MemberAddressFeignDTO;
import com.whoiszxl.query.MemberAddressQueryApplicationService;
import com.whoiszxl.query.model.response.MemberAddressResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户地址查询应用服务接口实现
 *
 * @author whoiszxl
 * @date 2022/1/24
 */
@Service
@RequiredArgsConstructor
public class MemberAddressQueryApplicationServiceImpl implements MemberAddressQueryApplicationService {

    private final MemberAddressMapper memberAddressMapper;

    @Override
    public List<MemberAddressResponse> list() {
        long memberId = StpUtil.getLoginIdAsLong();
        List<MemberAddressPO> memberAddressPOList = memberAddressMapper.selectList(
                Wrappers.<MemberAddressPO>lambdaQuery()
                        .eq(MemberAddressPO::getMemberId, memberId)
                        .orderByDesc(MemberAddressPO::getUpdatedAt)
                        .orderByDesc(MemberAddressPO::getIsDefault)
        );
        List<MemberAddressResponse> memberAddressResponseList = cn.hutool.core.bean.BeanUtil.copyToList(memberAddressPOList, MemberAddressResponse.class);
        return memberAddressResponseList;
    }

    @Override
    public MemberAddressFeignDTO getMemberAddress(Long memberId, Long addressId) {
        LambdaQueryWrapper<MemberAddressPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberAddressPO::getMemberId, memberId);
        queryWrapper.eq(MemberAddressPO::getId, addressId);
        MemberAddressPO memberAddressPO = memberAddressMapper.selectOne(queryWrapper);

        MemberAddressFeignDTO memberAddressFeignDTO = BeanUtil.copyProperties(memberAddressPO, MemberAddressFeignDTO.class);
        return memberAddressFeignDTO;
    }
}
